package servlets;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import capaLogica.Noticia;
import capaLogica.NoticiaBO;
import capaLogica.Usuario;

/**
 * Servlet implementation class NoticiaCO
 */
public class NoticiaCO extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticiaCO() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String accion = request.getParameter("accion");
		try {
			procesarAccion(accion, request, response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void procesarAccion(String accion, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, ParseException {
		if (accion.equals("crearNoticia")) {
			crearNoticia(request, response);
		}else if ( accion.equals("borrarNoticia")){
			borrarNoticia(request,response);
		}else if ( accion.equals("listarNoticias")){
			listarNoticias(request,response);
		}
	}

	private void listarNoticias(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NoticiaBO noticiaBO = new NoticiaBO();
		List<Noticia> listaNoticias = noticiaBO.recuperarTodas();
		request.setAttribute("noticias", listaNoticias);
		request.getRequestDispatcher("noticias.jsp").include(request, response);
		
	}

	private void borrarNoticia(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String idNoticia = request.getParameter("idNoticia");
		
		NoticiaBO noticiaBO = new NoticiaBO();
		noticiaBO.borrarNoticia(idNoticia);
		List<Noticia> listaNoticias = noticiaBO.recuperarTodas();
		request.setAttribute("noticias", listaNoticias);
		request.getRequestDispatcher("noticias.jsp").include(request, response);
		
	}

	private void crearNoticia(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession();
		Usuario user = (Usuario)sesion.getAttribute("usuario");
		
		String descripcion = request.getParameter("descripcion");
		
		NoticiaBO noticiaBO = new NoticiaBO();
		Noticia noticia = new Noticia();
		noticia.setDescripcion(descripcion);
		
		// Para la fecha de hoy
		java.util.Date today = new java.util.Date();
		java.sql.Date hoy = new java.sql.Date(today.getTime());	
		
		noticia.setFechaPublicacion(hoy);
		noticia.setIdUsuarioCreador(user.getIdUsuario());
		
		noticiaBO.insertarNoticia(noticia);
		List<Noticia> listaNoticias = noticiaBO.recuperarTodas();
		request.setAttribute("noticias", listaNoticias);
		request.getRequestDispatcher("noticias.jsp").include(request, response);
	}

}
